# -*- coding: utf-8 -*-
from django.http import HttpResponse

from sprt.schedule.models import ScheduleType
from sprt.admin.models import Dept

import datetime
 
#nav_list=[{"/my/overview":"概述"},{"/schedule":"计划"},{"/review":"回顾"},{"/approved":"审批"},{"/checkin":"考勤"},{"/admin":"管理"}]

def createExtInfo(request,context):
    #global nav_list
    #context["nav_list"]=nav_list
    context["full_path"]=request.get_full_path()
    context["management_role"]=request.META["management_role"]
    if request.user.is_authenticated():
#        scheduleTypes=ScheduleType.objects.all()
#        context["schedule_type_list"]=scheduleTypes
        context["user_detail"]=request.user.get_profile()
    else:
        context["user_detail"]=None
    context["current_full_url"]="http://%s%s" %(request.META["HTTP_HOST"], request.get_full_path())
    
def getInt(key, default, **kwargs):
    if kwargs == None:
        return None
    if not kwargs.has_key(key):
        return None
    v = kwargs[key]
    if (v == None or len(v)==0):
        return default
    return int(v)

def getString(key, **kwargs):
    if kwargs == None:
        return None
    if not kwargs.has_key(key):
        return None
    return kwargs[key]

def get_date_range(dt1_str, dt2_str):
#    dt1_str = request.POST.get('dt1')
#    dt2_str = request.POST.get('dt2')
    dt1 = dt2 = None
    if dt1_str!=None and len(dt1_str) > 0 and dt2_str !=None and len(dt2_str) > 0:
        try:
            dt1 = datetime.datetime.strptime(dt1_str,'%Y-%m-%d')
            dt2 = datetime.datetime.strptime(dt2_str,'%Y-%m-%d')
        except ValueError:
            pass
    if dt1 == None or dt2 == None:
        dt1 = datetime.date(datetime.date.today().year,datetime.date.today().month,1) #first day of current month.
        dt2 = datetime.date.today()
    return dt1, dt2


def get_my_dept_tree(user, my_href, branch_href, leaf_href):
    my_dept_qs = Dept.objects.filter(principal_auth_user=user)
    if my_dept_qs == None or len(my_dept_qs) == 0:
        return
    html = ''
    for dept in my_dept_qs:
        html = '%s<li><a href="%s%d" target="rframe">%s</a>\n' % (html, my_href, dept.id, dept.name)
        html = get_dept_tree(dept,html,0,branch_href,leaf_href)
        html = html + '</li>\n'
#    print html
    return html

def get_dept_tree(a_dept,html,depth,branch_href, leaf_href):
    from sprt.accounts.models import UserDetail
    user_list_qs = UserDetail.objects.filter(dept=a_dept)
    html = get_dept_user(user_list_qs, html, depth+1, leaf_href)
    qs = Dept.objects.filter(superior_dept=a_dept)
    if qs != None and len(qs) > 0:
        if user_list_qs==None or len(user_list_qs) == 0:
            html='%s%s<ul>\n'%(html,depth*"  ")
        for d in qs:
            html='%s%s<li><a href="%s%d" target="rframe">%s</a>\n' %(html,(depth+1)*"  ",branch_href,d.id,d.name)
            html= get_dept_tree(d,html,depth+1,branch_href,leaf_href)
            html = '%s%s</li>\n' %(html,(depth+1)*"  ")
        html = '%s%s</ul>\n' %(html, depth*"  ")
    else:
        if user_list_qs!=None or len(user_list_qs) > 0:
            html = '%s%s</ul>\n' %(html, depth*"  ")
    return html

def get_dept_user(qs, html, depth, leaf_href):
    if qs != None and len(qs) > 0:
        html='%s%s<ul>\n'%(html,depth*"  ")
        for u in qs:
            html = '%s%s<li><a href="%s%d" target="rframe">%s</a></li>\n' %(html, (depth+1) * "  ", leaf_href, u.id, u.user.username)
#        html='%s%s</ul>\n'%(html,depth*"  ")
    return html

def get_200_response(nav_tab_id, callback_type, forward_url):
    return HttpResponse(u'{"statusCode":"200","message":"操作成功","navTabId":"%s","callbackType":"%s","forwardUrl":"%s"}' % (nav_tab_id, callback_type, forward_url))
